﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace QLBV_Nhom8
{
    public partial class frmThanhToanMacDinh : Form
    {
        public frmThanhToanMacDinh()
        {
            InitializeComponent();
        }

        private void frmThanhToanMacDinh_Load(object sender, EventArgs e)
        {
            btnXacNhan.Enabled = false;
            btnInPhieu.Enabled = false;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            PhieuKhamBenhBUS.ThanhToanMacDinh(txtPhieuKham.Text, txtTienKham.Text);
            btnTim_Click(sender, e);
            btnXacNhan.Enabled = false;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                sql = string.Format(@"select MaPKB, MaPhieu, NgayKham, HoTen, ThanhToanMacDinh from PhieuKhamBenh, HoSoBenhNhan
                where PhieuKhamBenh.MaHoSoBenhNhan = HoSoBenhNhan.MaHoSoBenhNhan and (MaPhieu = '{0}' or HoTen like N'%{0}%' )", txtSearch.Text);
            }
            else
            {
                sql = string.Format(@"select MaPKB, MaPhieu, NgayKham, HoTen, ThanhToanMacDinh from PhieuKhamBenh, HoSoBenhNhan
                where PhieuKhamBenh.MaHoSoBenhNhan = HoSoBenhNhan.MaHoSoBenhNhan and YEAR(NgayKham) = YEAR(GetDate()) and MONTH(NgayKham) = MONTH(GetDate()) and DAY(NgayKham) = DAY(GetDate())");
            }
            dataGridView1.DataSource = PhieuKhamBenhBUS.TimKiemThanhToanMacDinh(sql);
            DinhDang();
        }

        private void DinhDang()
        {
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].HeaderText = "Mã Phiếu";
            dataGridView1.Columns[2].HeaderText = "Ngày Khám";
            dataGridView1.Columns[3].HeaderText = "Họ Tên";
            dataGridView1.Columns[4].HeaderText = "Số Tiền";
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                txtPhieuKham.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                txtNgayKham.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString().Substring(0, 10);
                txtHoTen.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                txtTienKham.Text = "40000";

                if (Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[4].Value) > 0)
                {
                    btnXacNhan.Enabled = false;
                    btnInPhieu.Enabled = false;
                }
                else
                {
                    btnXacNhan.Enabled = true;
                    btnInPhieu.Enabled = true;
                }
            }
        }
    }
}
